var id;
var urlType = ly.getUrlParam("type");
var roleArr = []; // 角色列表
var clinicArr = []; // 诊所

$(function() {
  id = ly.getUrlParam("id");
  
  if (urlType == 1) {
    // 新增
    getRoleScreen(); // 获取角色列表-筛选
    getClinic(); // 获取诊所列表
    $("#type").getSelect({
      asName: "type",
      type: "select",
      list: [{ value: 0, display: '普通管理员' },{ value: 1, display: '超级管理员' }]
    });
    $("#status").getSelect({
      type: "radio",
      asName: "status",
      list: [{ "value": "0", "display": "禁止登陆" }, { "value": "1", "display": "允许登陆" }],
      default: 1
    })
  } else {
    $(".title span").text("编辑");
    $(".form").getFormTwo({
      id: id
    }, function(data) {
      getClinic(data.hospitalId); // 获取诊所列表
      getRoles(data.roleId); // 获取角色列表
      $("#type").getSelect({
        asName: "type",
        type: "select",
        list: [{ value: 0, display: '普通管理员' },{ value: 1, display: '超级管理员' }],
        default: data.type
      });
      $("#status").getSelect({
        type: "radio",
        asName: "status",
        list: [{ "value": "0", "display": "禁止登陆" }, { "value": "1", "display": "允许登陆" }],
        default: data.status
      });

      $("#roleId").attr("disabled", "disabled");

    });
  }
  //提交
  $("body").on("click", "#save-btn", function(event) {
    event.preventDefault();
    var success = $('.form').validate('submitValidate');

    var passwordLength = $("#password").val().length;
    var isRightPass;
    if (passwordLength >= 6 && passwordLength <= 24) {
      isRightPass = true;
    } else {
      isRightPass = false;
    }

    if (success) {
      if (isRightPass) {
        //验证通过提交表单
        submitFom();
      } else {
        ly.toast("密码输入有误，请检查密码位数是否为6~24位");
      }
    }
  })

});

// 获取角色列表
function getRoles(defaultId) {
  roleArr = [];
  request({
    url: site.apiUrl + '/roleManager/selectRoleInfoList',
    type: "get",
    data: {
      page: 1,
      pageSize: 5000
    },
    callback: function(f) {
      if (f.code == 'success') {
        var forData = f.result.list
        for (var i = 0; i < forData.length; i++) {
          roleArr.push({
            value: forData[i].id,
            display: forData[i].roleName
          })
        }
        $("#roleId").getSelect({
          asName: "roleId",
          type: "select",
          list: roleArr,
          default: defaultId
        });
      } else {
        ly.toast(f.desc);
      }
    }
  })
}

// 获取角色列表-筛选
function getRoleScreen() {
  roleArr = [];
  request({
    url: site.apiUrl + '/roleManager/selectRoleInfoList',
    type: "get",
    data: {
      page: 1,
      pageSize: 5000
    },
    callback: function(f) {
      if (f.code == 'success') {
        var forData = f.result.list
        for (var i = 0; i < forData.length; i++) {
          if ([1, 3].indexOf(forData[i].id) === -1) {
            roleArr.push({
              value: forData[i].id,
              display: forData[i].roleName
            })
          }
        }
        $("#roleId").getSelect({
          asName: "roleId",
          type: "select",
          list: roleArr
        });
      } else {
        ly.toast(f.desc);
      }
    }
  })
}

// 获取诊所列表
function getClinic(defaultId) {
  clinicArr = [];
  request({
    url: site.apiUrl + '/hospitalManager/selectMedicalHospitalInfoList',
    type: "get",
    data: {
      page: 1,
      pageSize: 5000
    },
    callback: function(f) {
      if (f.code == "success") {
        var forData = f.result.list;
        if (forData.length > 0) {
          for (var i = 0; i < forData.length; i++) {
            clinicArr.push({
              value: forData[i].id,
              display: forData[i].hospitalName
            })
          }
        } else {
          clinicArr.push({value: "", display: "暂无分类"})
        }
        $("#hospitalId").getSelect({
          asName: "hospitalId",
          type: "select",
          list: clinicArr,
          default: defaultId
        });
      }
    }
  })
}

/*
 * 提交表单
 */
function submitFom() {
  var userName = $("#userName").val();
  var telephone = $("#telephone").val();
  var password = $("#password").val();
  var hospitalId = $("#hospitalId").val();
  var roleId = $("#roleId").val();
  var status = $("#status").find('input[name=status]:checked').val();

  var obj = {};
  var url = site.apiUrl + "/accountManager/addAdminAccount";
	obj = {
		'userName': userName,
    'telephone': telephone,
    'password': password,
    'hospitalId': hospitalId,
    'roleId': roleId,
    'status': status,
    // 'token': window.localStorage.getItem("backstage-access-token"),
    'createUser': window.localStorage.getItem("backstage-access-userName")
  };
  // if (password.length < 24) {
  //   obj.password = md5(password);
  // } else {
  //   obj.password = password;
  // }
  // 修改
  if (urlType == 2) {
    var thisId = $("#currentId").val();
    obj.id = thisId;
  }
  request({
    url: url,
    data: obj,
    type: "get",
    callback: function(f) {
      if (f.code == 200) {
        if (urlType == 1) {
          ly.alert('保存成功', function() {
            ly.goto("list.html");
          });
        } else {
          ly.alert('编辑成功', function() {
            ly.goto("list.html");
          });
        }
      } else {
        ly.toast(f.msg);
      }
    }
  });
}